package link.xieyu.service;

import link.xieyu.mapper.AppNoticedMapper;
import link.xieyu.mapper.AppUsersMapper;
import link.xieyu.pojo.AppNoticed;
import link.xieyu.pojo.vo.FansVO;
import link.xieyu.pojo.vo.NoticedVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class AppNoticedServiceImpl implements AppNoticedService {
    @Autowired
    private AppNoticedMapper appNoticedMapper;
    @Autowired
    private AppUsersMapper usersMapper;

    @Override
    public Object findFans(Integer id, String keyword, Integer sort) {
        List<NoticedVO> allByBeNoticed = appNoticedMapper.findAllByBeNoticed(id, keyword);
//        排序0默认1总额2次数
        return Objects.equals(sort, 1) ? allByBeNoticed
                .stream()
                .sorted(Comparator.comparingInt(AppNoticed::getBuyPrice))
                .collect(Collectors.toList())
                : Objects.equals(sort, 2) ?
                allByBeNoticed.stream()
                        .sorted(Comparator.comparingInt(AppNoticed::getBuyCount))
                        .collect(Collectors.toList()) : allByBeNoticed;
    }

    @Override
    public Object findAttention(Integer id) {
        return appNoticedMapper.findAllByUserId(id);
    }

    @Override
    public void addNoticed(AppNoticed appNoticed) {
        appNoticedMapper.insertSelective(appNoticed);
    }

    @Override
    public void delNoticed(Integer userId, Integer id) {
        appNoticedMapper.deleteByUserIdAndBeNoticed(userId, id);
    }

    @Override
    public void prohibit(Integer userId, Integer fansId) {
        appNoticedMapper
                .updateIsDelByBeNoticedAndUserId(
                        appNoticedMapper.findOneIsDelByBeNoticedAndUserId(userId, fansId) == 0 ? 1 : 0, userId, fansId
                );
    }
}
